package cn.yqn.system.controller;

import cn.yqn.basic.annotation.PreAuthorize;
import cn.yqn.basic.utils.JsonResult;
import cn.yqn.basic.utils.PageList;
import cn.yqn.system.domain.DictionaryType;
import cn.yqn.system.query.DictionaryTypeQuery;
import cn.yqn.system.service.IDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class DictionaryTypeController {

    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    @GetMapping
    public List<DictionaryType> loadAll() {
        return dictionaryTypeService.loadAll();
    }

    @GetMapping("/{id}")
    public DictionaryType loadById(@PathVariable("id") Long id) {
        return dictionaryTypeService.loadById(id);
    }

    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") Long id) {
        try {
            dictionaryTypeService.remove(id);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PatchMapping
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            System.out.println(ids);
            dictionaryTypeService.patchDel(ids);
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PutMapping
    public JsonResult saveOrUpdate(@RequestBody DictionaryType dictionaryType) {
        try {
            if (dictionaryType.getId() == null) {
                dictionaryTypeService.save(dictionaryType);
            } else{
                dictionaryTypeService.update(dictionaryType);
            }
            return JsonResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }
    @PostMapping
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery dictionaryTypeQuery) {
        try {
            return dictionaryTypeService.pageQuery(dictionaryTypeQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<DictionaryType>();
        }
    }
}
